package com.example.project22_editingmediaplayer.adapter;

import java.util.ArrayList;
import java.util.Random;

import com.example.project22_editingmediaplayer.R;
import com.example.project22_editingmediaplayer.objects.ArtisObject;
import com.example.project22_editingmediaplayer.resource.SongResource;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;



public class RowArtis extends BaseAdapter {

	public ArrayList<ArtisObject> listData;
	private LayoutInflater mInflater;
	private Context mContext;

	public RowArtis(ArrayList<ArtisObject> listData, Context mContext) {
		this.listData = listData;
		mInflater = (LayoutInflater) mContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext = mContext;
	}

	public void resetData(ArrayList<ArtisObject> listData) {
		this.listData = listData;
		this.notifyDataSetChanged();
	}

	public ArrayList<ArtisObject> getAllData()
	{
		return this.listData;
	}
	
	public void addData(ArtisObject songObject) {
		listData.add(songObject);
		this.notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listData.size();
	}

	@Override
	public ArtisObject getItem(int postition) {
		// TODO Auto-generated method stub
		return listData.get(postition);
	}

	@Override
	public long getItemId(int postition) {
		// TODO Auto-generated method stub
		return postition;
	}

	@Override
	public View getView(int postition, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		RowHolder holder = null;
		View view = convertView;
		if (view == null) {
			view = mInflater.inflate(R.layout.row_item, null);
			holder = new RowHolder();
			holder.imvAvarSong = (ImageView) view
					.findViewById(R.id.imvAvarSong);
			holder.txvSong = (TextView) view.findViewById(R.id.txtNameSong);
			holder.txvArtis = (TextView) view.findViewById(R.id.txtArtis);
			view.setTag(holder);
		} else {
			holder = (RowHolder) view.getTag();
		}
		final ArtisObject artisObject = listData.get(postition);
		holder.txvSong.setText(artisObject.get_artis()
				+ " ("
				+ String.valueOf(artisObject.getNumberTracks())
				+ " "
				+ mContext.getApplicationContext().getResources()
						.getString(R.string.number_song) + ")");
		holder.txvArtis.setText(artisObject.getNumberAlbum() + " "
				+ mContext.getResources().getString(R.string.album));
		// set image view
		Random rand = new Random();
		int currentSongIndex = rand.nextInt(SongResource.musicBackGround.length);
		holder.imvAvarSong.setBackgroundResource(SongResource.musicBackGround[currentSongIndex]);
		return view;
	}

	public static final class RowHolder {
		public ImageView imvAvarSong;
		public TextView txvSong;
		public TextView txvArtis;
	}
}
